package com.cntaiping.ttnews.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cntaiping.ttnews.R;
import com.cntaiping.ttnews.bean.NewsBean;
import com.squareup.picasso.Picasso;

/**
 * Created by Administrator on 2016/7/21.
 */
public class MyMoreTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_ONE = 0;
    private static final int TYPE_TWO = 1;
    private static final int TYPE_THREE = 2;

    private NewsBean mdatas;
    private Context context;

    public MyMoreTypeAdapter(Context context, NewsBean mdatas ) {
        this.context = context;
        this.mdatas = mdatas;
    }

    @Override
    public int getItemViewType(int position) {
        return mdatas.getNewslist().get(position).getPicShowType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_ONE:
                View itemViewOne = LayoutInflater.from(context).inflate(R.layout.item_one, parent, false);
                return new OneTypeHolder(itemViewOne);
            case TYPE_TWO:
                View itemViewTwo = LayoutInflater.from(context).inflate(R.layout.item_two, parent, false);
                return new TwoTypeHolder(itemViewTwo);
            case TYPE_THREE:
                View itemViewThree = LayoutInflater.from(context).inflate(R.layout.item_three, parent, false);
                return new ThreeTypeHolder(itemViewThree);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        NewsBean.NewslistBean newslistBean = mdatas.getNewslist().get(position);

            switch (viewType) {
                case TYPE_ONE:
                    OneTypeHolder oneTypeHolder = (OneTypeHolder) holder;
                    Picasso.with(context).load(newslistBean.getThumbnails_qqnews().get(0)).into(oneTypeHolder.iv_pic);
                    oneTypeHolder.tv_title.setText(newslistBean.getTitle());
                    oneTypeHolder.tv_source.setText(newslistBean.getSource());
                    oneTypeHolder.tv_pcount.setText(mdatas.getChangeInfo().getSubIdComments().get(position).getComments()+"评");
                    break;
                case TYPE_TWO:
                    TwoTypeHolder twoTypeHolder = (TwoTypeHolder) holder;
                    Picasso.with(context).load(newslistBean.getThumbnails_qqnews().get(0)).into(twoTypeHolder.iv_pic_two);
                    twoTypeHolder.tv_title_two.setText(newslistBean.getTitle());
                    twoTypeHolder.tv_source_two.setText(newslistBean.getSource());
                    twoTypeHolder.tv_pcount_two.setText(mdatas.getChangeInfo().getSubIdComments().get(position).getComments()+"评");
                    break;

                case TYPE_THREE:
                    ThreeTypeHolder threeTypeHolder = (ThreeTypeHolder) holder;
                    Picasso.with(context).load(newslistBean.getThumbnails_qqnews().get(0)).into(threeTypeHolder.iv_item_three_one);
                    Picasso.with(context).load(newslistBean.getThumbnails_qqnews().get(2)).into(threeTypeHolder.iv_item_three_two);
                    Picasso.with(context).load(newslistBean.getThumbnails_qqnews().get(3)).into(threeTypeHolder.iv_item_three_three);
                    threeTypeHolder.tv_title_three.setText(newslistBean.getTitle());
                    threeTypeHolder.iv_item_three_source.setText(newslistBean.getSource());
                    threeTypeHolder.iv_item_three_pcount.setText(mdatas.getChangeInfo().getSubIdComments().get(position).getComments()+"评");
                    break;


            }
        }


    @Override
    public int getItemCount() {
        return mdatas.getNewslist()==null?0:mdatas.getNewslist().size();
    }

    class OneTypeHolder extends RecyclerView.ViewHolder {

        public ImageView iv_pic;
        public TextView tv_title;
        public TextView tv_source;
        public TextView tv_time;
        public TextView tv_pcount;

        public OneTypeHolder(View itemView) {
            super(itemView);

            iv_pic = (ImageView) itemView.findViewById(R.id.iv_pic);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_source = (TextView) itemView.findViewById(R.id.tv_source);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_pcount = (TextView) itemView.findViewById(R.id.tv_pcount);
        }
    }

    class TwoTypeHolder extends RecyclerView.ViewHolder {


        public TextView tv_title_two;
        public ImageView iv_pic_two;
        public TextView tv_source_two;
        public TextView tv_pcount_two;

        public TwoTypeHolder(View itemView) {
            super(itemView);
            tv_title_two = (TextView) itemView.findViewById(R.id.tv_title_two);
            iv_pic_two = (ImageView) itemView.findViewById(R.id.iv_pic_two);
            tv_source_two = (TextView) itemView.findViewById(R.id.tv_source_two);
            tv_pcount_two = (TextView) itemView.findViewById(R.id.tv_pcount_two);


        }
    }

    class ThreeTypeHolder extends RecyclerView.ViewHolder {

        public TextView tv_title_three;
        public ImageView iv_item_three_one;
        public ImageView iv_item_three_two;
        public ImageView iv_item_three_three;
        public TextView iv_item_three_source;
        public TextView iv_item_three_pcount;

        public ThreeTypeHolder(View itemView) {
            super(itemView);
            tv_title_three = (TextView) itemView.findViewById(R.id.tv_title_three);
            iv_item_three_one = (ImageView) itemView.findViewById(R.id.iv_item_three_one);
            iv_item_three_two = (ImageView) itemView.findViewById(R.id.iv_item_three_two);
            iv_item_three_three = (ImageView) itemView.findViewById(R.id.iv_item_three_three);
            iv_item_three_source = (TextView) itemView.findViewById(R.id.iv_item_three_source);
            iv_item_three_pcount = (TextView) itemView.findViewById(R.id.iv_item_three_pcount);
        }
    }


}
